package com.cn.dim.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.dim.pojo.Pages;
import com.cn.dim.pojo.Proptype;
import com.cn.dim.service.ProptypeService;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/proptype")
public class ProptypeController {

	@Resource
	private ProptypeService proptypeService;
	
	@RequestMapping(value ="/queryProptype",method = RequestMethod.GET)
	@ResponseBody
	//Pages是自己定义的分页类，此方法用于带条件查询显示宣传类别列表
	public Map<String,Object> queryProptype(HttpServletRequest request,Proptype p,Pages pages){
		//参数u是列表页中以查询条件数据临时组成的过渡Proptype对象 tr
		Map<String,Object> param=new HashMap<>();//定义查询参数in
		Map<String,Object> reMap=new HashMap<>();//定义返回值，其中有一项必须是集合，先按照下面的写法做
		param.put("pageIndex", pages.getPage());
		param.put("pageSize", pages.getRows());
		param.put("proptype", p);
		
		try {
			Page<Map<String,Object>> rList = (Page)proptypeService.queryProptype(param);
			reMap.put("rows", rList);
			reMap.put("total", rList.getTotal());
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/insertProptype",method = RequestMethod.POST)
	@ResponseBody
	//此方法用于添加一个宣传类别
	public Map<String,Object> insertProptype(Proptype p)
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		String proptypeid=com.cn.dim.utils.StringUtil.makeUUID();
		p.setId(proptypeid);//主键
		p.setIsdel("0");//构造删除标记位
		
        try 
		{
			int flag = proptypeService.insertProptype(p);
			if(flag>0)
			{
				reMap.put("message", "插入成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/updateProptype",method = RequestMethod.POST)
	@ResponseBody
	//此方法用于添加一个宣传类别
	public Map<String,Object> updateProptype(Proptype p,String proptypeid) 
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
        p.setId(proptypeid);
        try 
		{
			int flag = proptypeService.updateProptype(p);
			if(flag>0)
			{
				reMap.put("message", "修改成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/delProptype",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> delProptype(HttpServletRequest request,String proptypeid) 
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
        Map<String,Object> m=new HashMap<>();//构造del方法的参数
        m.put("proptypeid", proptypeid);
		try 
		{
			int flag = proptypeService.delProptype(m);
			if(flag>0)
			{
				reMap.put("message", "删除成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/queryProptypeById",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> queryProptypeById(HttpServletRequest request,String proptypeid){
		Map<String,Object> m=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		m.put("proptypeid", proptypeid);
		try {
			Proptype proptype =proptypeService.queryProptypeById(m);
			reMap.put("data", proptype);
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/getProptypeInWx",method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String,Object>> getProptypeInWx(HttpServletRequest request){
		Map<String,Object> param=new HashMap<>();
		List<Map<String, Object>> rList = new ArrayList<>();
		try {
			rList = proptypeService.getProptypeInWx(param);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return rList;
	}
}
